package xyz.jcat.spring.security.example.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.jcat.common.web.Resp;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public Resp hello() {
        return Resp.ok("hello spring security");
    }

    @GetMapping("/login/user1")
    public Resp loginUser1() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return Resp.ok(principal);
    }

    @GetMapping("/login/user2")
    public Resp loginUser2(Authentication authentication) {
        return Resp.ok(authentication.getPrincipal());
    }

    @GetMapping("/login/user3")
    public Resp loginUser3(@AuthenticationPrincipal UserDetails userDetails) {
        return Resp.ok(userDetails);
    }
}
